package com.example.finalhomework.secondhomework

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.example.finalhomework.R

val expert = ProgramExpert()
@Composable
fun SecondHomework(navController: NavHostController, expert: ProgramExpert = ProgramExpert()) {
    // 计数器状态
    var count by remember { mutableStateOf(0) }
    // 选中的语言状态
    var selectedLanguage by remember { mutableStateOf("") }
    var displayedLanguage by remember { mutableStateOf("") }
    // 语言列表
    val languages = listOf("快速", "容易", "新语言", "面向对象")
    // 动态添加的TextView列表
    val textViewList = remember { mutableStateListOf<String>() }
    // 滚动状态
    val listState = rememberLazyListState()

    LaunchedEffect(textViewList.size) {
        listState.animateScrollToItem(textViewList.size)
    }

    Box(
        modifier = Modifier
            .fillMaxSize()
    ) {
        Image(
            painter = painterResource(id = R.drawable.background2),
            contentDescription = null,
            contentScale = ContentScale.Crop,
            modifier = Modifier.fillMaxSize()
        )
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(16.dp)
        ) {
            // 按钮，点击时计数器加一并添加新的TextView
            Button(onClick = {
                count++
                textViewList.add("This is a new TextView $count")
                Log.d("SecondHomework", "Button clicked, count incremented to: $count")
            }) {
                Text(text = stringResource(R.string.secondwork_button1))
            }

            Spacer(modifier = Modifier.height(16.dp))

            // 设置固定高度的滚动视图
            LazyColumn(
                state = listState,
                modifier = Modifier
                    .fillMaxWidth()
                    //                .height(200.dp) // 设置固定高度
                    .fillMaxHeight(0.5f)
            ) {
                // 显示动态添加的TextView列表
                items(textViewList) { text ->
                    Text(text = text)
                    Spacer(modifier = Modifier.height(16.dp))
                }
            }

            Spacer(modifier = Modifier.height(16.dp))

            // 下拉菜单选择器
            Spinner(
                items = languages,
                selectedItem = selectedLanguage,
                onItemSelected = { selectedLanguage = it }
            )

            Spacer(modifier = Modifier.height(16.dp))

            // 按钮，点击时根据选中的语言获取对应的编程语言
            Button(onClick = {
                displayedLanguage = expert.getLanguage(selectedLanguage)
                Log.d("SecondHomework", "Language selected: $selectedLanguage, displayedLanguage: $displayedLanguage")
            }) {
                Text(text = stringResource(R.string.secondwork_button2))
            }

            // 显示选中的语言
            Box(modifier = Modifier.fillMaxWidth()) {
                Text(
                    text = displayedLanguage,
                    fontSize = 30.sp,
                    modifier = Modifier.align(Alignment.Center)
                )
            }
        }
    }
    Log.d("SecondHomework", "Composable function executed")
}

@Composable
fun Spinner(items: List<String>, selectedItem: String, onItemSelected: (String) -> Unit) {
    // 下拉菜单是否展开的状态
    var expanded by remember { mutableStateOf(false) }
    Box {
        // 显示选中的项目或提示选择项目
        Text(
            text = selectedItem.ifEmpty { "Select an item" },
            modifier = Modifier
                .fillMaxWidth()
                .clickable(
                    onClick = { expanded = true },
                    interactionSource = remember { MutableInteractionSource() },
                    indication = null
                )
                .background(Color.White, shape = RoundedCornerShape(8.dp))
                .border(1.dp, Color.Black, shape = RoundedCornerShape(8.dp))
                .padding(16.dp)
        )

        // 下拉菜单
        DropdownMenu(
            expanded = expanded,    //设置下拉菜单是否展开
            onDismissRequest = { expanded = false },    //设置下拉菜单消失时的回调
            modifier = Modifier
                .background(Color.White, shape = RoundedCornerShape(8.dp))
                .border(1.dp, Color.Black, shape = RoundedCornerShape(8.dp))
        ) {
            // 遍历每个项目
            items.forEach { item ->
                // 创建下拉菜单项
                DropdownMenuItem(
                    text = { Text(text = item) },
                    onClick = {
                        // 选中项目时调用回调函数
                        onItemSelected(item)
                        // 关闭下拉菜单
                        expanded = false
                        Log.d("Spinner", "Item selected: $item")
                    }
                )
            }
        }
    }
}

@Preview
@Composable
fun SecondHomeworkPreview() {
    SecondHomework(navController = NavHostController(LocalContext.current))
}